﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Input;

using CustomControlDatabaseInterface.Model;
using CustomControlDatabaseInterface.Utilities;

namespace CustomControlDatabaseInterface.ViewModel
{
    public class CustomerDetailsViewModel : WorkspaceViewModel {

        private string customerName;

        public String CustomerName {
            get {
                return customerName;
            }
            set {
                if (value != null) {
                    customerName = value;
                    OnPropertyChanged("CustomerName");
                }
            }
        }

        public ObservableCollection<CustomerDetailTechnologyViewModel> CustomerTechControls { get; set; }

        private bool isAddTechVisible; 

        public bool IsAddTechVisible {
            get {
                return isAddTechVisible; 
            }
            set {
                isAddTechVisible = value;
                IsAddTechButtonEnabled = !value; 
                OnPropertyChanged("IsAddTechVisible");
            }
        }

        public bool isAddTechButtonEnabled;

        public bool IsAddTechButtonEnabled 
        {
            get {
                return isAddTechButtonEnabled;
            }
            set {
                isAddTechButtonEnabled = value; 
                OnPropertyChanged("IsAddTechButtonEnabled");
            }
        }

        public CustomerDetailAddTechViewModel AddTechViewModel { get; set; }

        public ICommand AddTechnologyCommand { get; set; }

        public ICommand UndoChangesCommand { get; set; }

        public ICommand SaveChangesCommand { get; set; }

        public CustomerDetailsViewModel() {
            CustomerName = null; 
            CustomerTechControls = new ObservableCollection<CustomerDetailTechnologyViewModel>();

            IsAddTechVisible = false;
            AddTechViewModel = new CustomerDetailAddTechViewModel();
            AddTechnologyCommand = new RelayCommand(param => OnAddTechnologyCommand());
            UndoChangesCommand = new RelayCommand(param => OnUndoChangesCommand());
            SaveChangesCommand = new RelayCommand(param => OnSaveChangesCommand());

            AddTechViewModel.InitializeOnAddTechViewModel(ConvertTechControlsToStringList());
            AddTechViewModel.AddTech += OnTechnologyAdded;
            AddTechViewModel.Cancel += delegate {
                CustomerTechControls.Remove(AddTechViewModel);
                IsAddTechButtonEnabled = true;
            };

        }


        public void InititializeFromCustomerName(String customerName) {
            CustomerName = customerName;
            CustomerTechControls.Clear();
            List<String> allTechnologies = new List<string>();
            allTechnologies = DatabaseUtil.GetTableNames(); 
            foreach (String tech in allTechnologies) {
                CustomerDetailTechnologyViewModel techTechnology = new CustomerDetailTechnologyViewModel(CustomerName, tech);
                if (techTechnology.Controls.Count > 0) {
                    CustomerTechControls.Add(techTechnology);
                }
            }     
        }

        private void OnAddTechnologyCommand()
        {
            CustomerTechControls.Add(AddTechViewModel);
            IsAddTechButtonEnabled = false;
        }

        private void OnTechnologyAdded(String technology) {
            CustomerTechControls.Remove(AddTechViewModel);
            CustomerTechControls.Add(new CustomerDetailTechnologyViewModel(CustomerName, technology));
            AddTechViewModel.InitializeOnAddTechViewModel(ConvertTechControlsToStringList());
            IsAddTechButtonEnabled = true;

        }


        private List<String> ConvertTechControlsToStringList() {
            List<String> result = new List<string>();
            foreach (CustomerDetailTechnologyViewModel item in CustomerTechControls) {
                result.Add(item.Technology);
            }
            return result; 
        }

        private void OnSaveChangesCommand() {
            foreach (CustomerDetailTechnologyViewModel item in CustomerTechControls) {
                item.UpdateCustomerTechControls();
            }
        }

        private void OnUndoChangesCommand() {
            InititializeFromCustomerName(CustomerName);
        }
    }
}
